package net.wanho.controller;

import net.wanho.entity.User;
import net.wanho.service.UserService;
import net.wanho.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * Author：汤小洋
 * Date：2024-08-08 16:41
 * Description：<描述>
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    // @ResponseBody
    public AjaxResult login(){
        // System.out.println("用户登录。。。。。" + userService);
        // userService.login("tom","123");

        User user = new User(1001, "admin", "123", 18, null, new Date());

        return AjaxResult.success(user);
    }

    @RequestMapping("/logout")
    // @ResponseBody
    public String logout(){
        return userService.logout();
    }

    @RequestMapping("/param1")
    public void param1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println(req.getParameter("name"));

        PrintWriter out = resp.getWriter();
        out.print("success");
        out.close();
    }

    @RequestMapping("/param2")
    public String param2(@RequestParam("username") String name,@RequestParam(required = false, defaultValue = "20") int age, Double height){
        System.out.println(name + "---" + age + "---" + height);

        return "success";
    }

    @RequestMapping("/param3/{id}/{name}")
    public String param3(@PathVariable("id") Integer id, @PathVariable String name){
        System.out.println(id + "---" + name);
        return "success";
    }

    @RequestMapping("/param4")
    public String param4(@RequestHeader("User-Agent") String userAgent, @CookieValue("aliyungf_tc") String aliyungf){
        System.out.println(userAgent);
        System.out.println(aliyungf);
        return "success";
    }

    @RequestMapping("/param5")
    // public String param4(@RequestBody String str){
    // public String param4(@RequestBody User user){
    public String param4(@RequestBody Integer[] ids){
        // System.out.println(str);

        // JSONObject obj = JSONUtil.parseObj(str);
        // String name = (String) obj.get("name");

        // System.out.println(user);
        System.out.println(ids);

        return "success";
    }

    @RequestMapping("/user/add")
    public AjaxResult addUser(@RequestBody User user){
        System.out.println(user);
        return AjaxResult.success();
    }

    @RequestMapping("/user/remove/{id}")
    public AjaxResult removeUser(@PathVariable Integer id){
        System.out.println(id);
        return AjaxResult.success();
    }

    @PostMapping("/param6")
    // @RequestMapping(value = "/param6", method = RequestMethod.POST)
    // public String param6(@ModelAttribute User user){
    public String param6(@RequestBody User user){
        System.out.println(user);
        return "success";
    }


}
